/**
 * @author luguoxiang
 * @date 2022/9/17
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
import { h } from 'vue';
import More from './More.vue';
import { IComponentProps } from '@/soar';

export interface LgButtonListProp extends IComponentProps {
  /**
   * 配置子项
   * @param slots
   */
  children?(slots: any): any[];

  /**
   * 显示按钮数量
   */
  limit?: number | string;
}

// 插槽：default，trigger
export const LgButtonList = (props: LgButtonListProp, context: any) => {
  const { slots } = context;
  let children = props.children ? props.children(slots) : slots.default();
  const limit = props.limit || 2;

  // 按钮列表处理
  children = children.filter((x: any) => x.children !== 'v-if');
  if (children.length > limit) {
    // @ts-ignore
    const index = limit - 1;
    const list = [index];
    for (let i = 0; i < index; i++) {
      list[i] = children[i];
    }
    // @ts-ignore
    list.push(h(More, { children, index }, { default: () => [slots.trigger ? slots.trigger()[0] : '更多'] }));
    return h('button-list', props, list);
  }
  return h('button-list', props, children);
};
